package com.blog.service;

import com.blog.basic.query.BasicQuery;
import com.blog.basic.query.PageList;
import com.blog.dao.ArticleDao;
import com.blog.dao.CategoryDao;
import com.blog.entity.Article;
import com.blog.entity.Category;
import javafx.util.Builder;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ArticleService {
    @Autowired
   private ArticleDao articleDao;
    @Autowired
  private  CategoryDao categoryDao;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 查询所有博客 时间降序排序
     * @return
     */
    public List<Article> articleList(){
        List<Article> articles = articleDao.selectAll();

//        for (Article article : articles) {
//            String format = sdf.format(article.getDate());
//            article.setDate(format);
//        }
        return articles;
    }
    /**
     * 根据id删除博客内容
     * @param id
     */
    public void deleteArticleById(Integer id) {
        articleDao.deleteById(id);
    }
    /**
     * 根据id查询博客内容
     * @param id
     * @return
     */
    public Article getArticleById(Integer id) {
        Article article = articleDao.selectById(id);
        article.setCategory(categoryDao.selectById(article.getCategoryId()).getDisplayName());
        return article;
    }
    /**
     * 查询导航栏全部
     * @return
     */
    public List<Category> getCategories() {
        return categoryDao.selectAll();
    }
    /**
     * 发布博客
     * @param article
     */
    @Transactional
    public void writeBlog(Article article) {
      System.out.println(article);
        Integer id=categoryDao.selectByCategoryName(article.getCategory());
        System.out.println(id);
        article.setCategoryId(id);

        articleDao.save(article);
    }

    /**
     * 修改博客
     * @param article
     */
    public void updateBlog(Article article) {
//        article.setTime();
        System.out.println(article);
        Integer id=categoryDao.selectByCategoryName(article.getCategory());
        System.out.println("sss"+id);
        article.setCategoryId(id);
        articleDao.update(article);
    }
    /**
     * 导航栏条件查询
     * @param name
     * @return
     */
    public List<Article> getArticlesByCategoryName(String name) {
        Integer categoryId = articleDao.getCategoryIdByName(name);
        List<Article> articles = articleDao.getArticlesByCategoryName(categoryId);
        return articles;
    }

    /**
    * @Description: 分页加查询
    * @Param:
    * @return:
    * @Author: Mr.Zhao
    * @Date: 2022/4/5
    */
    public PageList<Article> getListPage(BasicQuery basicQuery) {
       //创建自己封装的泪
        PageList<Article> pageList = new PageList<>();

        //查询所有自己需要的
        List<Article> articles = articleDao.getbBasicQuery(basicQuery);
        //查询总数
      Integer totals=  articleDao.getTotals(basicQuery);

      pageList.setList(articles);
      pageList.setTotals(totals);

      return pageList;


    }
}
